
package com.neegix.log.exceptionLog.interfaces.controller;

import com.neegix.base.PageVO;
import com.neegix.exception.BusinessRuntimeException;
import com.neegix.result.Result;
import com.neegix.log.exceptionLog.application.assembler.ExceptionLogAssembler;
import com.neegix.log.exceptionLog.application.cqrs.query.ExceptionLogQueryRepository;
import com.neegix.log.exceptionLog.domain.entity.ExceptionLogEntity;
import com.neegix.log.exceptionLog.interfaces.vo.ExceptionLogVO;
import com.neegix.log.exceptionLog.application.service.ExceptionLogService;
import com.neegix.log.exceptionLog.interfaces.form.NewExceptionLogForm;
import com.neegix.log.exceptionLog.interfaces.form.QueryExceptionLogForm;
import com.neegix.log.exceptionLog.interfaces.form.UpdateExceptionLogForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.access.prepost.PreAuthorize;

import java.util.List;
import java.util.Optional;

 /**
  * This file is auto-generated by nebula-framework-generator.
  * The auto-generation plugin was developed using IntelliJ IDEA Community Edition.
  * Thanks to JetBrains for their outstanding contributions to the developer community.
  * <p>
  * The code generated by this tool is owned by the user of the tool.
  * The tool itself is copyrighted by <a href="https://www.neegix.com">https://www.neegix.com</a>.
  *
  * @author <a href="https://www.neegix.com">https://www.neegix.com</a>
  * @version 1.0.0
  * @since 2024-12-19 15:04:54
  */

@RestController
@RequestMapping("/api/v1/exceptionLog")
public class ExceptionLogController {

    @Autowired
    private ExceptionLogQueryRepository exceptionLogQueryRepository;

    @Autowired
    private ExceptionLogService exceptionLogService;

    @PreAuthorize("hasAuthority('log:exceptionLog:add')")
    @PostMapping
    public Result<Void> createExceptionLog(@RequestBody NewExceptionLogForm exceptionLogForm){
        Void result = exceptionLogService.createExceptionLog(ExceptionLogAssembler.INSTANCE.covertEntity(exceptionLogForm));
        return Result.success("创建成功", result);
    }

    @PreAuthorize("hasAuthority('log:exceptionLog:modify')")
    @PutMapping("/{id}")
    public Result<Void> updateExceptionLog(@PathVariable Long id, @RequestBody UpdateExceptionLogForm exceptionLogForm){
        ExceptionLogEntity exceptionLogEntity = ExceptionLogAssembler.INSTANCE.covertEntity(exceptionLogForm);
        exceptionLogEntity.setId(id);
        Void result = exceptionLogService.modifyExceptionLog(exceptionLogEntity);
        return Result.success("更新成功",result);
    }

    @PreAuthorize("hasAuthority('log:exceptionLog:list')")
    @GetMapping("/{currentPage}/{pageSize}")
    public Result<PageVO<ExceptionLogVO>> getExceptionLogs(@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize, @ModelAttribute QueryExceptionLogForm exceptionLogForm){
        if(exceptionLogForm == null) {
            exceptionLogForm = new QueryExceptionLogForm();
        }
        PageVO<ExceptionLogVO> pageVO = exceptionLogQueryRepository.findPage(currentPage, pageSize, ExceptionLogAssembler.INSTANCE.covertDTO(exceptionLogForm));
        return Result.success("查询成功",pageVO);
    }

    @PreAuthorize("hasAuthority('log:exceptionLog:get')")
    @GetMapping("/{id}")
    public Result<ExceptionLogVO> getExceptionLogById(@PathVariable("id") Long id) {
        Optional<ExceptionLogVO> optional = exceptionLogQueryRepository.findById(id);
        return Result.success("获取成功", optional.orElseThrow(()-> new BusinessRuntimeException("查询结果不存在")));
    }

    @PreAuthorize("hasAuthority('log:exceptionLog:remove')")
    @DeleteMapping
    public Result<Void> removeExceptionLog(@RequestBody List<Long> ids){
        return Result.success("删除成功", exceptionLogService.removeExceptionLog(ids));
    }
}